package com.gjw.planeWar;

import com.gjw.style.Style;
import lombok.Data;

import java.awt.*;

/**
 * @author Administrator
 * @since 2019/4/29.
 * 子弹类
 */
@Data
public class Bullet extends GameObject {
    // 子弹速度
    private static final int SPEED = 5;
    // 子弹大小
    private static Style style = Style.getINSTANCE();
    public static int WIDTH = style.bullet.getWidth();
    public static int HEIGHT = style.bullet.getHeight();
    private Rectangle rect = new Rectangle();
    GameModel gm = GameModel.getInstance();
    //    private static final Props props = Props.getInstance();
    // 子弹是否存活（出界）
    private boolean living = true;

    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;

        gm.add(this);

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;

    }


    // 子弹类自己画自己
    @Override
    public void paint(Graphics g) {
        if (!gm.heroPlane.isLiving()) {
            living = false;
        }
        if (!living) {
            gm.remove(this);
        }
        move();
        g.drawImage(style.bullet, x, y, null);
    }

    private void move() {
        y -= SPEED;
        rect.x = this.x;
        rect.y = this.y;
        // 超出边界，该子弹不存活
        if (y < 0) {
            this.die();
        }
    }


    public void die() {
        this.living = false;
    }
}
